/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iodegree.lib.devpModel;

import iodegree.lib.CommonsLib;
import iodegree.lib.D;
import java.awt.Toolkit;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.SwingWorker;

/**
 *
 * @author Libra
 */
public class ModelUI extends javax.swing.JFrame {

    /**
     * Creates new form ModelViewUI
     */
    public ModelUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        modelGrp = new javax.swing.ButtonGroup();
        clusterGrp = new javax.swing.ButtonGroup();
        triQuadGrp = new javax.swing.ButtonGroup();
        ioTypeGrp = new javax.swing.ButtonGroup();
        ioGrp = new javax.swing.ButtonGroup();
        maxDistGrp = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        btnInitModel = new javax.swing.JButton();
        btnStopModel = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtGluFac = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtGABAFac = new javax.swing.JTextField();
        chkDepolarGABA = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();
        txtIterFac = new javax.swing.JTextField();
        rdoRand = new javax.swing.JRadioButton();
        rdoBiDir = new javax.swing.JRadioButton();
        rdoUniDir = new javax.swing.JRadioButton();
        txtGenPairTIme = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        btnShow = new javax.swing.JButton();
        btnStart = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        rdo300Less = new javax.swing.JRadioButton();
        rdo300More = new javax.swing.JRadioButton();
        txtNeuronNum = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtConnProb = new javax.swing.JTextField();
        txtWeightScale = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        chkWriteFile = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        btnCluster = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        btnCommNeiTest = new javax.swing.JButton();
        chkFwdGlu = new javax.swing.JCheckBox();
        chkRevGlu = new javax.swing.JCheckBox();
        rdoGlu = new javax.swing.JRadioButton();
        rdoGABA = new javax.swing.JRadioButton();
        rdoBoth = new javax.swing.JRadioButton();
        rdoTri = new javax.swing.JRadioButton();
        rdoQuad = new javax.swing.JRadioButton();
        txtGenGrpTime = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnSplit = new javax.swing.JButton();
        chkFreq = new javax.swing.JCheckBox();
        chkRepeat = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        rdoIOGlu = new javax.swing.JRadioButton();
        rdoGABAIO = new javax.swing.JRadioButton();
        rdoInput = new javax.swing.JRadioButton();
        rdoOutput = new javax.swing.JRadioButton();
        btnIODegree = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JSeparator();
        btnGlobalDegree = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtPrg = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnInitModel.setText("Init");
        btnInitModel.setPreferredSize(new java.awt.Dimension(85, 25));
        btnInitModel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInitModelActionPerformed(evt);
            }
        });

        btnStopModel.setText("Stop");
        btnStopModel.setPreferredSize(new java.awt.Dimension(85, 25));
        btnStopModel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopModelActionPerformed(evt);
            }
        });

        jLabel2.setText("Model Construct:");

        jLabel1.setText("Glu Factor ");

        txtGluFac.setEditable(false);
        txtGluFac.setText("1.0");
        txtGluFac.setPreferredSize(new java.awt.Dimension(40, 20));

        jLabel3.setText("GABA Factor");

        txtGABAFac.setEditable(false);
        txtGABAFac.setText("1.0");
        txtGABAFac.setPreferredSize(new java.awt.Dimension(40, 20));

        chkDepolarGABA.setSelected(true);
        chkDepolarGABA.setText("Depolar GABA");

        jLabel5.setText("Iteration Factor");

        txtIterFac.setText("1000");
        txtIterFac.setPreferredSize(new java.awt.Dimension(40, 20));
        txtIterFac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIterFacActionPerformed(evt);
            }
        });

        modelGrp.add(rdoRand);
        rdoRand.setText("Random");
        rdoRand.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoRandActionPerformed(evt);
            }
        });

        modelGrp.add(rdoBiDir);
        rdoBiDir.setText("ActiveBiDir");
        rdoBiDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoBiDirActionPerformed(evt);
            }
        });

        modelGrp.add(rdoUniDir);
        rdoUniDir.setSelected(true);
        rdoUniDir.setText("ActiveOutGoing");
        rdoUniDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoUniDirActionPerformed(evt);
            }
        });

        txtGenPairTIme.setText("15");
        txtGenPairTIme.setPreferredSize(new java.awt.Dimension(25, 20));
        txtGenPairTIme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtGenPairTImeActionPerformed(evt);
            }
        });

        jLabel7.setText("s sampling");

        btnShow.setText("Show");
        btnShow.setEnabled(false);
        btnShow.setPreferredSize(new java.awt.Dimension(85, 25));
        btnShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowActionPerformed(evt);
            }
        });

        btnStart.setText("Start");
        btnStart.setPreferredSize(new java.awt.Dimension(85, 25));
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        btnExit.setText("Exit");
        btnExit.setPreferredSize(new java.awt.Dimension(85, 25));
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator3.setPreferredSize(new java.awt.Dimension(10, 100));

        jLabel8.setText("Maximum Intercellular Distance");

        maxDistGrp.add(rdo300Less);
        rdo300Less.setText("300");
        rdo300Less.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdo300LessActionPerformed(evt);
            }
        });

        maxDistGrp.add(rdo300More);
        rdo300More.setSelected(true);
        rdo300More.setText("500");

        txtNeuronNum.setText("1000");
        txtNeuronNum.setPreferredSize(new java.awt.Dimension(40, 20));

        jLabel9.setText("Neurons");

        txtConnProb.setText("1.0");
        txtConnProb.setPreferredSize(new java.awt.Dimension(40, 20));

        txtWeightScale.setText("1.0");
        txtWeightScale.setPreferredSize(new java.awt.Dimension(40, 20));

        jLabel10.setText("Connection Prob Scale");

        jLabel11.setText("Syn Weight Scale");

        chkWriteFile.setSelected(true);
        chkWriteFile.setText("Write");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtGluFac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtGABAFac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(chkDepolarGABA)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtIterFac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtNeuronNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rdoRand)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rdoBiDir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rdoUniDir))
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rdo300Less)
                        .addGap(18, 18, 18)
                        .addComponent(rdo300More)))
                .addGap(18, 18, 18)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtWeightScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtConnProb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnInitModel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnStopModel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnShow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(chkWriteFile))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtGenPairTIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtGenPairTIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtConnProb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtWeightScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnInitModel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnStopModel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnShow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chkWriteFile))
                        .addGap(12, 12, 12))
                    .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNeuronNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rdoRand)
                            .addComponent(rdoBiDir)
                            .addComponent(rdoUniDir))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel1)
                            .addComponent(txtGluFac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(txtGABAFac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(chkDepolarGABA)
                            .addComponent(txtIterFac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rdo300Less)
                            .addComponent(rdo300More))
                        .addGap(9, 9, 9))))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnCluster.setText("Cluster");
        btnCluster.setPreferredSize(new java.awt.Dimension(85, 25));
        btnCluster.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClusterActionPerformed(evt);
            }
        });

        jLabel4.setText("Common Neighbor:");

        btnCommNeiTest.setText("Test");
        btnCommNeiTest.setPreferredSize(new java.awt.Dimension(85, 25));
        btnCommNeiTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCommNeiTestActionPerformed(evt);
            }
        });

        chkFwdGlu.setText("fwdGlu");

        chkRevGlu.setText("revGlu");

        clusterGrp.add(rdoGlu);
        rdoGlu.setText("Glu");

        clusterGrp.add(rdoGABA);
        rdoGABA.setText("GABA");

        clusterGrp.add(rdoBoth);
        rdoBoth.setSelected(true);
        rdoBoth.setText("Both");

        triQuadGrp.add(rdoTri);
        rdoTri.setSelected(true);
        rdoTri.setText("3");

        triQuadGrp.add(rdoQuad);
        rdoQuad.setText("4");

        txtGenGrpTime.setText("5");
        txtGenGrpTime.setPreferredSize(new java.awt.Dimension(25, 20));

        jLabel6.setText("s sampling");

        btnSplit.setText("Split");
        btnSplit.setPreferredSize(new java.awt.Dimension(85, 25));
        btnSplit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSplitActionPerformed(evt);
            }
        });

        chkFreq.setSelected(true);
        chkFreq.setText("Freq");

        chkRepeat.setSelected(true);
        chkRepeat.setText("Repeat x5");

        ioTypeGrp.add(rdoIOGlu);
        rdoIOGlu.setSelected(true);
        rdoIOGlu.setText("glutamate");

        ioTypeGrp.add(rdoGABAIO);
        rdoGABAIO.setText("GABA");

        ioGrp.add(rdoInput);
        rdoInput.setSelected(true);
        rdoInput.setText("Input");

        ioGrp.add(rdoOutput);
        rdoOutput.setText("Output");

        btnIODegree.setText("IO Deg");
        btnIODegree.setPreferredSize(new java.awt.Dimension(85, 25));
        btnIODegree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIODegreeActionPerformed(evt);
            }
        });

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);

        btnGlobalDegree.setText("Degrees");
        btnGlobalDegree.setPreferredSize(new java.awt.Dimension(85, 28));
        btnGlobalDegree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGlobalDegreeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCommNeiTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(chkFwdGlu)
                        .addGap(18, 18, 18)
                        .addComponent(chkRevGlu)
                        .addGap(18, 18, 18)
                        .addComponent(txtGenGrpTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(rdoBoth)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rdoGlu)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rdoGABA)
                                .addGap(18, 18, 18))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(btnCluster, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(78, 78, 78)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(rdoTri)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rdoQuad))
                            .addComponent(chkFreq)
                            .addComponent(chkRepeat)))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnSplit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                        .addComponent(jSeparator2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdoIOGlu)
                            .addComponent(rdoInput))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdoOutput)
                            .addComponent(rdoGABAIO)))
                    .addComponent(btnIODegree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnGlobalDegree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator4)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnGlobalDegree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(rdoBoth)
                                    .addComponent(rdoGlu)
                                    .addComponent(rdoGABA)
                                    .addComponent(rdoTri)
                                    .addComponent(rdoQuad))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnCluster, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(chkFreq))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnSplit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(chkRepeat))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(chkFwdGlu)
                                    .addComponent(chkRevGlu)
                                    .addComponent(txtGenGrpTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCommNeiTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(rdoIOGlu)
                                    .addComponent(rdoGABAIO))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(rdoInput)
                                    .addComponent(rdoOutput))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnIODegree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 7, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txtPrg.setColumns(20);
        txtPrg.setRows(5);
        jScrollPane1.setViewportView(txtPrg);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnInitModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInitModelActionPerformed
        initModel();
        btnInitModel.setEnabled(false);
    }//GEN-LAST:event_btnInitModelActionPerformed

    private void btnClusterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClusterActionPerformed
        es.execute(clusterWorker());
    }//GEN-LAST:event_btnClusterActionPerformed

    private void btnCommNeiTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCommNeiTestActionPerformed
//        ModelProbe probe = new ModelProbe(m0);
        es.execute(commNeiborWoker());
    }//GEN-LAST:event_btnCommNeiTestActionPerformed

    private void btnStopModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopModelActionPerformed
        m0.stop();
//        es.shutdown();
    }//GEN-LAST:event_btnStopModelActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        if (null != es) {
            es.shutdown();
        }
        if (null != circuitView) {
            circuitView.closeWindow();
        }
    }//GEN-LAST:event_formWindowClosed

    private void rdoRandActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoRandActionPerformed
        modelType = ModelType.Ctrl;
        circuitView.setNetworkType(modelType);
    }//GEN-LAST:event_rdoRandActionPerformed

    private void rdoBiDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoBiDirActionPerformed
        modelType = ModelType.NetworkBiDir;
        circuitView.setNetworkType(modelType);
    }//GEN-LAST:event_rdoBiDirActionPerformed

    private void rdoUniDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoUniDirActionPerformed
        modelType = ModelType.Network;
        circuitView.setNetworkType(modelType);
    }//GEN-LAST:event_rdoUniDirActionPerformed

    private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowActionPerformed
//        es.execute(modelDisplayWorker());
        circuitView.setNetworkType(modelType);
        circuitView.setVisible(true);
    }//GEN-LAST:event_btnShowActionPerformed

    private void txtGenPairTImeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGenPairTImeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtGenPairTImeActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        m0.setType(modelType);
        m0.setConnProbScale(Float.parseFloat(txtConnProb.getText()));
        m0.setWeightScale(Float.parseFloat(txtWeightScale.getText()));
        m0.setWriteFile(chkWriteFile.isSelected());
        m0.setDEPOLAR_GABA(chkDepolarGABA.isSelected());
//        m0.setLessThan300(rdo300Less.isSelected());
        m0.setRunning();
        notifyWaiting(waiting);
    }//GEN-LAST:event_btnStartActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        m0.setExit();
        notifyWaiting(waiting);
        btnInitModel.setEnabled(true);
    }//GEN-LAST:event_btnExitActionPerformed

    private void notifyWaiting(AtomicBoolean waiting) {
        synchronized (waiting) {
            waiting.set(false);
            waiting.notify();
        }
    }

    private void btnSplitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSplitActionPerformed
        split();      // TODO add your handling code here:
    }//GEN-LAST:event_btnSplitActionPerformed

    private void btnIODegreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIODegreeActionPerformed
        es.execute(ioDegreeWoker());
    }//GEN-LAST:event_btnIODegreeActionPerformed

    private void btnGlobalDegreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGlobalDegreeActionPerformed
        es.execute(globalDegreeWorker());
    }//GEN-LAST:event_btnGlobalDegreeActionPerformed

    private void rdo300LessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdo300LessActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdo300LessActionPerformed

    private void txtIterFacActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIterFacActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIterFacActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ModelUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ModelUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCluster;
    private javax.swing.JButton btnCommNeiTest;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnGlobalDegree;
    private javax.swing.JButton btnIODegree;
    private javax.swing.JButton btnInitModel;
    private javax.swing.JButton btnShow;
    private javax.swing.JButton btnSplit;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStopModel;
    private javax.swing.JCheckBox chkDepolarGABA;
    private javax.swing.JCheckBox chkFreq;
    private javax.swing.JCheckBox chkFwdGlu;
    private javax.swing.JCheckBox chkRepeat;
    private javax.swing.JCheckBox chkRevGlu;
    private javax.swing.JCheckBox chkWriteFile;
    private javax.swing.ButtonGroup clusterGrp;
    private javax.swing.ButtonGroup ioGrp;
    private javax.swing.ButtonGroup ioTypeGrp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.ButtonGroup maxDistGrp;
    private javax.swing.ButtonGroup modelGrp;
    private javax.swing.JRadioButton rdo300Less;
    private javax.swing.JRadioButton rdo300More;
    private javax.swing.JRadioButton rdoBiDir;
    private javax.swing.JRadioButton rdoBoth;
    private javax.swing.JRadioButton rdoGABA;
    private javax.swing.JRadioButton rdoGABAIO;
    private javax.swing.JRadioButton rdoGlu;
    private javax.swing.JRadioButton rdoIOGlu;
    private javax.swing.JRadioButton rdoInput;
    private javax.swing.JRadioButton rdoOutput;
    private javax.swing.JRadioButton rdoQuad;
    private javax.swing.JRadioButton rdoRand;
    private javax.swing.JRadioButton rdoTri;
    private javax.swing.JRadioButton rdoUniDir;
    private javax.swing.ButtonGroup triQuadGrp;
    private javax.swing.JTextField txtConnProb;
    private javax.swing.JTextField txtGABAFac;
    private javax.swing.JTextField txtGenGrpTime;
    private javax.swing.JTextField txtGenPairTIme;
    private javax.swing.JTextField txtGluFac;
    private javax.swing.JTextField txtIterFac;
    private javax.swing.JTextField txtNeuronNum;
    private javax.swing.JTextArea txtPrg;
    private javax.swing.JTextField txtWeightScale;
    // End of variables declaration//GEN-END:variables

    private void initModel() {
        btnShow.setEnabled(false);
        pathToFile = CommonsLib.getDefaultFile();
        es = Executors.newCachedThreadPool();
        es.execute(modelCalcWorker());
        es.execute(modelDisplayWorker());
        es.execute(progressUpdateWorker());
    }

    private SwingWorker modelCalcWorker() {
        SwingWorker calcWorker;
        calcWorker = new SwingWorker<Void, Void>() {
            @Override
            public Void doInBackground() {
//                D.tp("calc worker started");
                try {
                    displaySem.acquire();
                } catch (InterruptedException e) {
                }
                float gluFac = Float.parseFloat(txtGluFac.getText());
                float gabaFac = Float.parseFloat(txtGABAFac.getText());
                float iterFac = Float.parseFloat(txtIterFac.getText());
                m0 = new ModelNew(gluFac, gabaFac, iterFac, waiting);
                m0.setLessThan300(rdo300Less.isSelected());
                m0.setTxtProg(txtPrg);
//                updateProgress("Running Model" + modelType.name());
                m0.setFile(pathToFile);
                int neuronNum = Integer.parseInt(txtNeuronNum.getText());
                m0.setCell(neuronNum, 8429, 0.76708864f);
//                m0.setSamplePairs(100000);
                m0.setGenMonitorTime(Integer.parseInt(txtGenPairTIme.getText()));
                m0.setEs(es);
                m0.init();
                displaySem.release();
                es.execute(m0);
                return null;
            }

            @Override
            protected void done() {
            }
        };
        return calcWorker;
    }

    private SwingWorker modelDisplayWorker() {
        SwingWorker modelWorker = new SwingWorker<Set<Integer>, HashSet<Integer>>() {
            
            
            
            @Override
            public Set<Integer> doInBackground() {
                circuitView = new CircuitViewUI();
                try {
                    displaySem.acquire();
                } catch (InterruptedException e) {
                }
                circuitView.setDim(m0.getDimension());
                circuitView.setCellList(m0.getCellList());
                circuitView.setMyWorker(this);
                btnShow.setEnabled(true);

                while (!m0.isFinished()) {
                    try {
                        publish(new HashSet<>(m0.getConnected()));
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println(e.toString());
                    }
                }
                
                displaySem.release();
                return m0.getConnected();
            }

            @Override
            protected void done() {
                if (circuitView.isVisible()) {
                    try {
                        circuitView.setConnected(get());
                    } catch (ExecutionException | InterruptedException e) {
                    }
                    circuitView.repaintCanvas();
                }
            }

            @Override
            protected void process(List<HashSet<Integer>> conns) {
                if (circuitView.isVisible()) {
                    circuitView.setConnected(conns.get(conns.size() - 1));
                    circuitView.repaintCanvas();
                }
            }
        };
        return modelWorker;
    }

    private SwingWorker progressUpdateWorker() {
        SwingWorker modelWorker = new SwingWorker<Void, Void>() {
            @Override
            public Void doInBackground() {
                try {
                    do {
                        Thread.sleep(500);
                    } while (null == m0);
                } catch (InterruptedException e) {
                }

                while (!m0.isFinished()) {
                    try {
                        newProgress = m0.getLastUpdate();
                        publish();
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
                return null;
            }

            @Override
            protected void done() {
                updateProgress(m0.getLastUpdate());
            }

            @Override
            protected void process(List<Void> v) {
                updateProgress();
            }
        };
        return modelWorker;
    }

    private SwingWorker clusterWorker() {
        SwingWorker clusterWorker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() {
                int type = rdoBoth.isSelected() ? 0 : rdoGlu.isSelected() ? 1 : 2;
                int size = rdoTri.isSelected() ? 3 : 4;
                int repeat = chkRepeat.isSelected() ? 5 : 1;

                while (repeat > 0) {
                    int[] histo = m0.probeCluster(Integer.parseInt(txtGenGrpTime.getText()), type, size);
//                D.tp("TYPE " + type + ", " + Arrays.toString(histo));
                    D.tpi(modelType + "\tTYPE_" + type + "\tGABA_" + (chkDepolarGABA.isSelected() ? "Depolar" : "HyperPolar"));
                    if (chkFreq.isSelected()) {
                        int total = 0;
                        for (int i = 0; i < histo.length; i++) {
                            total += histo[i];
                        }
                        for (int i = 0; i < histo.length; i++) {
                            D.tpi((double) histo[i] / total);
                        }
                    } else {
                        for (int i = 0; i < histo.length; i++) {
                            D.tpi(histo[i]);
                        }
                    }
                    D.tp();
                    repeat--;
                }
                split();
                return null;
            }
        };
        return clusterWorker;
    }

    private SwingWorker commNeiborWoker() {
        SwingWorker commNeiborWorker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() {
                int repeat = chkRepeat.isSelected() ? 5 : 1;
                while (repeat > 0) {
                    m0.probeCommNeib(
                            Integer.parseInt(txtGenGrpTime.getText()),
                            chkFwdGlu.isSelected(), chkRevGlu.isSelected());
                    repeat--;
                }
                split();
                return null;
            }
        };
        return commNeiborWorker;
    }

    private SwingWorker ioDegreeWoker() {
        SwingWorker ioWorker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() {
                int repeat = chkRepeat.isSelected() ? 5 : 1;
                while (repeat > 0) {
                    int[] degrees = m0.probeIO(
                            Integer.parseInt(txtGenGrpTime.getText()),
                            rdoIOGlu.isSelected(), rdoInput.isSelected());

                    D.tpi((rdoInput.isSelected() ? "Input" : "output"),
                            (rdoIOGlu.isSelected() ? "Glu" : "GABA"));
                    if (chkFreq.isSelected()) {
                        int total = 0;
                        for (int i = 0; i < degrees.length; i++) {
                            total += degrees[i];
                        }
                        for (int i = 0; i < degrees.length; i++) {
                            D.tpi((double) degrees[i] / total);
                        }
                    } else {
                        for (int i = 0; i < degrees.length; i++) {
                            D.tpi(degrees[i]);
                        }
                    }
                    D.tp();
                    repeat--;
                }
                split();
                return null;
            }
        };
        return ioWorker;
    }

    private SwingWorker globalDegreeWorker() {
        SwingWorker globalDegreeWorker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() {
                m0.probeGlobalDegrees();
                split();
                return null;
            }
        };
        return globalDegreeWorker;
    }

    private void split() {
        D.tp("===========================================");
        Toolkit.getDefaultToolkit().beep();
    }

    private void updateProgress() {
        String s = newProgress + "\r\n";
        if (!lastProgress.equals(s)) {
            txtPrg.append(s);
            lastProgress = s;
        }
    }

    private void updateProgress(String s) {
        s += "\r\n";
        if (!lastProgress.equals(s)) {
            txtPrg.append(s);
            lastProgress = s;
        }
    }
    private ModelNew m0;
    final private Semaphore displaySem = new Semaphore(1);
//    private Semaphore reRunSem = new Semaphore(1);
    private ExecutorService es;
    private CircuitViewUI circuitView;
    private String pathToFile;
    private ModelType modelType = ModelType.Network;
    private String lastProgress = "";
    private String newProgress = "";
    final AtomicBoolean waiting = new AtomicBoolean(true);
}
